<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Search_Lucene
 * @subpackage Document
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Document.php 16543 2009-07-07 09:44:56Z yoshida@zend.co.jp $
 */


/** Zend_Search_Lucene_Field */
require_once 'Zend/Search/Lucene/Field.php';


/**
 * A Document is a set of fields. Each field has a name and a textual value.
 *
 * @category   Zend
 * @package    Zend_Search_Lucene
 * @subpackage Document
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Search_Lucene_Document
{

	/**
	 * Associative array Zend_Search_Lucene_Field objects where the keys to the
	 * array are the names of the fields.
	 *
	 * @var array
	 */
	protected $_fields = array();

	/**
	 * Field boost factor
	 * It's not stored directly in the index, but affects on normalization factor
	 *
	 * @var float
	 */
	public $boost = 1.0;

	/**
	 * Proxy method for getFieldValue(), provides more convenient access to
	 * the string value of a field.
	 *
	 * @param  $offset
	 * @return string
	 */
	public function __get($offset)
	{
		return $this->getFieldValue($offset);
	}


	/**
	 * Add a field object to this document.
	 *
	 * @param Zend_Search_Lucene_Field $field
	 * @return Zend_Search_Lucene_Document
	 */
	public function addField(Zend_Search_Lucene_Field $field)
	{
		$this->_fields[$field->name] = $field;

		return $this;
	}


	/**
	 * Return an array with the names of the fields in this document.
	 *
	 * @return array
	 */
	public function getFieldNames()
	{
		return array_keys($this->_fields);
	}


	/**
	 * Returns Zend_Search_Lucene_Field object for a named field in this document.
	 *
	 * @param string $fieldName
	 * @return Zend_Search_Lucene_Field
	 */
	public function getField($fieldName)
	{
		if (!array_key_exists($fieldName, $this->_fields)) {
			require_once 'Zend/Search/Lucene/Exception.php';
			throw new Zend_Search_Lucene_Exception("Field name \"$fieldName\" not found in document.");
		}
		return $this->_fields[$fieldName];
	}


	/**
	 * Returns the string value of a named field in this document.
	 *
	 * @see __get()
	 * @return string
	 */
	public function getFieldValue($fieldName)
	{
		return $this->getField($fieldName)->value;
	}

	/**
	 * Returns the string value of a named field in UTF-8 encoding.
	 *
	 * @see __get()
	 * @return string
	 */
	public function getFieldUtf8Value($fieldName)
	{
		return $this->getField($fieldName)->getUtf8Value();
	}
}
